package com.cjl.jvm.gc;

import java.lang.ref.WeakReference;

public class WeakRef {
    public static void collect() throws Exception{
        System.out.println("WeakRef.collect start");
        System.gc();
        System.out.println("WeakRef.collect end");
        Thread.sleep(2000);
    }
    public static void main(String[] args) throws Exception {
        System.out.println("创建弱引用");
        Referred stong = new Referred(5);
        WeakReference<Referred> weak = new WeakReference<>(stong);
        WeakRef.collect();

        System.out.println("WeakRef.main 删除弱引用");
        stong.allocation1 = null;
        stong = null;
        WeakRef.collect();

        if (weak.get() == null){
            System.out.println("WeakRef.main weak.get = null");
        }


        System.out.println("WeakRef.main END");

    }
}
